% The code is based on the following paper:
%   Chen, T. Y., Lin, Y. L., and Tzeng, L. Y., forthcoming.
%   Estimating probability weighting functions through option pricing bounds. Review of Asset Pricing Studies.
%
% Copyright: Tzu-Ying Chen, Yo-Lan Lin, Larry Y. Tzeng
% Date: January 30, 2024

clear
      
%% Setting      
BV_Target = {'UB'};  

%% Optimal Risk Preferences under Rank-Dependent Expected Utility
FileName = ['Summary_OPT_AllCR_AllPeriod_OP_UB'];
load(FileName, ...
     'Date_Monthly', ...
     'Index_CR', 'Index_OPT', 'OPT_TAB');
clear FileName 

OPT = nan(size(Date_Monthly, 1), length(Index_OPT));                       
OPT_DEV = nan(size(Date_Monthly, 1), 1);                                    
for d = 1:size(Date_Monthly, 1)
    OPT(d, :) = OPT_TAB(Index_CR, Index_OPT, d);   
    OPT_DEV(d, :) = OPT_TAB(Index_CR, end, d);
end 
clear Index_CR Index_OPT OPT_TAB
clear d

%% Optimal Risk Preferences under Rank-Dependent Expected Utility (Using Piecewise Linear)
FileName = ['Summary_PL_OPT_AllPeriod_OP_UB'];
load(FileName, ...
     'OPT_PL', 'OPT_Type_PL', ...
     'OPT_COND_PL')
clear FileName 

OPT_DEV_PL = OPT_COND_PL(:, end);
clear OPT_COND_PL

%% Four Specific Cases 
AllPeriod = [[19990317 19990416]; ...
	         [20030219 20030321]; ...
	         [20111019 20111118]; ...
	         [20090121 20090220]];
[~, Index_Location] = ismember(AllPeriod, Date_Monthly(:, [1 2]), 'rows');
Index_Location(Index_Location==0) = [];                                    
clear Date_Monthly

OPT = OPT(Index_Location, :);                                              
OPT_DEV = OPT_DEV(Index_Location, :);                                      
OPT_PL = OPT_PL(Index_Location, :);                                        
OPT_DEV_PL = OPT_DEV_PL(Index_Location, :);                                
OPT_Type_PL = OPT_Type_PL(Index_Location, :);                              
clear Index_Location

AllPeriod_Text = cell(size(AllPeriod, 1), 1);                              
for d = 1:size(AllPeriod, 1)
    AllPeriod_Text(d, 1) = cellstr(char(['\quad \qquad \qquad \qquad ' datestr(datenum(num2str(AllPeriod(d, 1)), 'yyyymmdd'), 'mmm dd, yyyy')]));
end
clear d

%% Plot Figure: Probability Weighting Function
AllP = linspace(0, 1, 1000);
NUM_PL = 10;
AllTP = linspace(0, 1, 1 + NUM_PL);
clear NUM_PL

% Plot Figure
h_PWF = figure;
for d = 1:size(AllPeriod, 1)
    FileName = ['Summary_OP_Grid_' num2str(AllPeriod(d, 1))];
    load(FileName, ...
         'Record');
    clear FileName

    Index = find((Record(:, 1)==1) & ...
                 (Record(:, 2)==1));
    VIORate = 1 - Record(Index, end - 1);                                                              
    clear Index Record

    subplot(2, 2, d)
    plot(AllP, P1998(AllP, OPT(d, 1), OPT(d, 2)), ...
         'LineStyle', '-', ...
         'LineWidth', 2, ...
         'Color', 'r')
    hold on       
    
    if OPT_Type_PL(d) > 0 
        plot(AllTP, FitPWF_PL(AllTP, OPT_PL(d, :), []), ...
             'LineStyle', '-', ...
             'LineWidth', 2, ...
             'Marker', '.', ...
             'MarkerSize', 15, ...
             'Color', 'b')
        hold on   
    else
    end

    plot(AllTP, AllTP, ...
         'LineStyle', ':', ...
         'LineWidth', 2, ...
         'Color', 'k')
    hold on
    grid on   

    h = legend('Prelec (1998)', ...
               'Piecewise Linear');
    set(h, 'FontSize', 15, ...
           'FontName', 'Times New Roman', ...
           'FontWeight', 'Bold', ...
           'Location', 'NorthWest', ...
           'Interpreter', 'Latex', ...
           'Box', 'Off')
    clear h
  
    h1 = xlabel('$p$');  
    h2 = ylabel('$v(p)$');
    h3 = title([AllPeriod_Text(d, :); '(percentage of violations under EU = ' num2str(roundn(100 * VIORate, - 2)) '\%)']); 
    set([h1 h2 h3], 'FontSize', 15, ...
                    'FontName', 'Times New Roman', ...
                    'FontWeight', 'Bold', ...
                    'Interpreter', 'Latex')
    clear h1 h2 h3    
 
    set(gca, 'XLim', [0 1], ...
             'XTick', 0:0.25:1, ...
             'FontSize', 15, ...
             'FontName', 'Times New Roman')         
    set(gca, 'YLim', [0 1], ...
             'YTick', 0:0.25:1, ...
             'FontSize', 15, ...
             'FontName', 'Times New Roman')    
    
    set(gca, 'Layer', 'Top')
    clear VIORate
end
clear d

X = 29.7;                                                                  
Y = 21.0;                                                                  
XMargin = 1;                                                               
YMargin = 1;                                                               
XSize = X - 2 * XMargin;                                                   
YSize = Y - 2 * YMargin;                                                   
set(h_PWF, 'PaperUnits', 'Centimeters')
set(h_PWF, 'PaperSize', [X Y])
set(h_PWF, 'PaperPosition', [XMargin YMargin XSize YSize])
set(h_PWF, 'PaperOrientation', 'Portrait')
clear AllPeriod AllPeriod_Text AllCR_Target h_PWF X Y XMargin YMargin XSize YSize
clear AllP AllTP OPT OPT_DEV OPT_PL OPT_DEV_PL OPT_Type_PL

%% Output
FileName = ['Figure_PWF_PL_UB'];
saveas(gcf, [FileName '.eps'], 'epsc')
print(FileName, '-dpdf', '-fillpage')
winopen([FileName '.pdf'])
clear FileName